package com.yayun.merchant.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.yayun.common.core.controller.BaseController;
import com.yayun.common.core.domain.AjaxResult;
import com.yayun.common.core.page.TableDataInfo;
import com.yayun.merchant.domain.OmsOrder;
import com.yayun.merchant.service.IOmsOrderService;
import com.yayun.common.utils.poi.ExcelUtil;
import javax.servlet.http.HttpServletResponse;
import com.yayun.common.utils.SecurityUtils;

@RestController
@RequestMapping("/merchant/order")
public class OmsOrderController extends BaseController {
    @Autowired
    private IOmsOrderService orderService;

    /**
     * 查询订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(OmsOrder order) {
        startPage();
        List<OmsOrder> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }

    /**
     * 获取订单详细信息
     */
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        OmsOrder order = orderService.selectOrderById(id);
        // 非管理员只能查看自己的订单
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId()) 
            && !order.getUserId().equals(SecurityUtils.getUserId())) {
            return error("没有权限访问该订单");
        }
        return success(order);
    }

    /**
     * 创建订单
     */
    @PostMapping
    public AjaxResult add(@RequestBody OmsOrder order) {
        OmsOrder createdOrder = orderService.createOrder(order);
        return success(createdOrder);
    }

    /**
     * 修改订单
     */
    @PutMapping
    public AjaxResult edit(@RequestBody OmsOrder order) {
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 删除订单
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(orderService.deleteOrderByIds(ids));
    }

    /**
     * 订单支付
     */
    @PostMapping("/pay/{orderId}")
    public AjaxResult pay(@PathVariable("orderId") Long orderId, @RequestParam("payType") Integer payType) {
        return toAjax(orderService.payOrder(orderId, payType));
    }

    /**
     * 订单发货
     */
    @PostMapping("/delivery/{orderId}")
    public AjaxResult delivery(@PathVariable("orderId") Long orderId, 
                             @RequestParam("deliveryCompany") String deliveryCompany,
                             @RequestParam("deliverySn") String deliverySn) {
        return toAjax(orderService.deliveryOrder(orderId, deliveryCompany, deliverySn));
    }

    /**
     * 确认收货
     */
    @PutMapping("/receive/{orderId}")
    public AjaxResult confirm(@PathVariable("orderId") Long orderId) {
        return toAjax(orderService.confirmReceiveOrder(orderId));
    }

    /**
     * 取消订单
     */
    @PutMapping("/cancel/{orderId}")
    public AjaxResult cancel(@PathVariable("orderId") Long orderId) {
        return toAjax(orderService.cancelOrder(orderId, "用户取消订单"));
    }

    /**
     * 通过订单编号取消订单
     */
    @PutMapping("/cancel/sn/{orderSn}")
    public AjaxResult cancelByOrderSn(@PathVariable("orderSn") String orderSn) {
        return toAjax(orderService.cancelOrderByOrderSn(orderSn, "用户取消订单"));
    }

    /**
     * 通过订单编号支付订单
     */
    @PostMapping("/pay/sn/{orderSn}")
    public AjaxResult payByOrderSn(@PathVariable("orderSn") String orderSn, @RequestParam("payType") Integer payType) {
        return toAjax(orderService.payOrderByOrderSn(orderSn, payType));
    }

    /**
     * 通过订单编号获取订单详细信息
     */
    @GetMapping("/sn/{orderSn}")
    public AjaxResult getInfoByOrderSn(@PathVariable("orderSn") String orderSn) {
        OmsOrder order = orderService.selectOrderByOrderSn(orderSn);
        // 非管理员只能查看自己的订单
        if (!SecurityUtils.isAdmin(SecurityUtils.getUserId()) 
            && !order.getUserId().equals(SecurityUtils.getUserId())) {
            return error("没有权限访问该订单");
        }
        return success(order);
    }

    /**
     * 通过订单编号查询订单
     */
    @GetMapping("/query/sn/{orderSn}")
    public AjaxResult queryOrderByOrderSn(@PathVariable("orderSn") String orderSn) {
        return success(orderService.selectOrderByOrderSn(orderSn));
    }

    /**
     * 更新订单备注
     */
    @PutMapping("/note")
    public AjaxResult updateNote(@RequestBody OmsOrder order) {
        return toAjax(orderService.updateOrderNote(order));
    }

    /**
     * 导出订单列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, OmsOrder order) {
        List<OmsOrder> list = orderService.selectOrderList(order);
        ExcelUtil<OmsOrder> util = new ExcelUtil<OmsOrder>(OmsOrder.class);
        util.exportExcel(response, list, "订单数据");
    }
} 